<?php
class ModelReportSale extends Model {
	public function getOrders($filter) {
		$sql = "SELECT MIN(tmp.date_added) AS date_start, MAX(tmp.date_added) AS date_end, COUNT(tmp.order_id) AS `orders`, SUM(tmp.products) AS products, SUM(tmp.tax) AS tax, SUM(tmp.total) AS total FROM (SELECT o.order_id, (SELECT SUM(op.quantity) FROM `@@order_product` op WHERE op.order_id = o.order_id GROUP BY op.order_id) AS products, (SELECT SUM(ot.value) FROM `@@order_total` ot WHERE ot.order_id = o.order_id AND ot.code = 'tax' GROUP BY ot.order_id) AS tax, o.total, o.date_added FROM `@@order` o";

		if ($filter->get('filter_order_status_id')) {
			$sql .= " WHERE o.order_status_id = " . (int)$filter->get('filter_order_status_id');
		} else {
			$sql .= " WHERE o.order_status_id > 0";
		}

		if ($filter->get('filter_date_start')) {
			$sql .= " AND DATE(o.date_added) >= '" . ES($filter->get('filter_date_start')) . "'";
		}

		if ($filter->get('filter_date_end')) {
			$sql .= " AND DATE(o.date_added) <= '" . ES($filter->get('filter_date_end')) . "'";
		}

		$sql .= " GROUP BY o.order_id) tmp";

		$group = $filter->get('filter_group', 'week');

		switch($group) {
			case 'day';
				$sql .= " GROUP BY DAY(tmp.date_added)";
				break;
			default:
			case 'week':
				$sql .= " GROUP BY WEEK(tmp.date_added)";
				break;
			case 'month':
				$sql .= " GROUP BY MONTH(tmp.date_added)";
				break;
			case 'year':
				$sql .= " GROUP BY YEAR(tmp.date_added)";
				break;
		}

		$sql .= " ORDER BY tmp.date_added DESC";

		$sql .= $filter->limit();

		$query = $this->db->query($sql);

		return $query->rows;
	}

	public function getTotalOrders($filter) {
		
		$group = $filter->get('filter_group', 'week');

		switch($group) {
			case 'day';
				$sql = "SELECT COUNT(DISTINCT DAY(date_added)) AS total FROM `@@order`";
				break;
			default:
			case 'week':
				$sql = "SELECT COUNT(DISTINCT WEEK(date_added)) AS total FROM `@@order`";
				break;
			case 'month':
				$sql = "SELECT COUNT(DISTINCT MONTH(date_added)) AS total FROM `@@order`";
				break;
			case 'year':
				$sql = "SELECT COUNT(DISTINCT YEAR(date_added)) AS total FROM `@@order`";
				break;
		}

		if ($filter->get('filter_order_status_id')) {
			$sql .= " WHERE order_status_id = " . (int)$filter->get('filter_order_status_id');
		} else {
			$sql .= " WHERE order_status_id > 0";
		}
		
		if ($filter->get('filter_date_start')) {
			$sql .= " AND DATE(date_added) >= '" . ES($filter->get('filter_date_start')) . "'";
		}
		
		if ($filter->get('filter_date_end')) {
			$sql .= " AND DATE(date_added) <= '" . ES($filter->get('filter_date_end')) . "'";
		}

		return $this->db->queryOne($sql);
	}

	public function getTaxes($filter) {
		$sql = "SELECT MIN(o.date_added) AS date_start, MAX(o.date_added) AS date_end, ot.title, SUM(ot.value) AS total, COUNT(o.order_id) AS `orders` FROM `@@order_total` ot LEFT JOIN `@@order` o ON (ot.order_id = o.order_id) WHERE ot.code = 'tax'";

		if ($filter->get('filter_order_status_id')) {
			$sql .= " AND o.order_status_id = " . (int)$filter->get('filter_order_status_id');
		} else {
			$sql .= " AND o.order_status_id > 0";
		}

		if ($filter->get('filter_date_start')) {
			$sql .= " AND DATE(o.date_added) >= '" . ES($filter->get('filter_date_start')) . "'";
		}

		if ($filter->get('filter_date_end')) {
			$sql .= " AND DATE(o.date_added) <= '" . ES($filter->get('filter_date_end')) . "'";
		}

		$group = $filter->get('filter_group', 'week');

		switch($group) {
			case 'day';
				$sql .= " GROUP BY ot.title, DAY(o.date_added)";
				break;
			default:
			case 'week':
				$sql .= " GROUP BY ot.title, WEEK(o.date_added)";
				break;
			case 'month':
				$sql .= " GROUP BY ot.title, MONTH(o.date_added)";
				break;
			case 'year':
				$sql .= " GROUP BY ot.title, YEAR(o.date_added)";
				break;
		}

		$sql .= $filter->limit();

		$query = $this->db->query($sql);

		return $query->rows;
	}

	public function getTotalTaxes($filter) {
		$sql = "SELECT COUNT(*) AS total FROM (SELECT COUNT(*) AS total FROM `@@order_total` ot LEFT JOIN `@@order` o ON (ot.order_id = o.order_id) WHERE ot.code = 'tax'";

		if ($filter->get('filter_order_status_id')) {
			$sql .= " AND o.order_status_id = " . (int)$filter->get('filter_order_status_id');
		} else {
			$sql .= " AND o.order_status_id > 0";
		}

		if ($filter->get('filter_date_start')) {
			$sql .= " AND DATE(o.date_added) >= '" . ES($filter->get('filter_date_start')) . "'";
		}

		if ($filter->get('filter_date_end')) {
			$sql .= " AND DATE(o.date_added) <= '" . ES($filter->get('filter_date_end')) . "'";
		}
		
		$group = $filter->get('filter_group', 'week');

		switch($group) {
			case 'day';
				$sql .= " GROUP BY DAY(o.date_added), ot.title";
				break;
			default:
			case 'week':
				$sql .= " GROUP BY WEEK(o.date_added), ot.title";
				break;
			case 'month':
				$sql .= " GROUP BY MONTH(o.date_added), ot.title";
				break;
			case 'year':
				$sql .= " GROUP BY YEAR(o.date_added), ot.title";
				break;
		}

		$sql .= ") tmp";

		return  $this->db->queryOne($sql);
	}

	public function getShipping($filter) {
		$sql = "SELECT MIN(o.date_added) AS date_start, MAX(o.date_added) AS date_end, ot.title, SUM(ot.value) AS total, COUNT(o.order_id) AS `orders` FROM `@@order_total` ot LEFT JOIN `@@order` o ON (ot.order_id = o.order_id) WHERE ot.code = 'shipping'";

		if ($filter->get('filter_order_status_id')) {
			$sql .= " AND o.order_status_id = " . (int)$filter->get('filter_order_status_id');
		} else {
			$sql .= " AND o.order_status_id > 0";
		}

		if ($filter->get('filter_date_start')) {
			$sql .= " AND DATE(date_added) >= '" . ES($filter->get('filter_date_start')) . "'";
		}

		if ($filter->get('filter_date_end')) {
			$sql .= " AND DATE(date_added) <= '" . ES($filter->get('filter_date_end')) . "'";
		}

		$group = $filter->get('filter_group', 'week');

		switch($group) {
			case 'day';
				$sql .= " GROUP BY ot.title, DAY(o.date_added)";
				break;
			default:
			case 'week':
				$sql .= " GROUP BY ot.title, WEEK(o.date_added)";
				break;
			case 'month':
				$sql .= " GROUP BY ot.title, MONTH(o.date_added)";
				break;
			case 'year':
				$sql .= " GROUP BY ot.title, YEAR(o.date_added)";
				break;
		}

		$sql .= $filter->limit();

		$query = $this->db->query($sql);

		return $query->rows;
	}

	public function getTotalShipping($filter) {
		$sql = "SELECT COUNT(*) AS total FROM (SELECT COUNT(*) AS total FROM `@@order_total` ot LEFT JOIN `@@order` o ON (ot.order_id = o.order_id) WHERE ot.code = 'shipping'";

		if ($filter->get('filter_order_status_id')) {
			$sql .= " AND order_status_id = " . (int)$filter->get('filter_order_status_id');
		} else {
			$sql .= " AND order_status_id > 0";
		}
		
		if ($filter->get('filter_date_start')) {
			$sql .= " AND DATE(date_added) >= '" . ES($filter->get('filter_date_start')) . "'";
		}
		
		if ($filter->get('filter_date_end')) {
			$sql .= " AND DATE(date_added) <= '" . ES($filter->get('filter_date_end')) . "'";
		}
		
		$group = $filter->get('filter_group', 'week');
		
		switch($group) {
			case 'day';
				$sql .= " GROUP BY DAY(o.date_added), ot.title";
				break;
			default:
			case 'week':
				$sql .= " GROUP BY WEEK(o.date_added), ot.title";
				break;
			case 'month':
				$sql .= " GROUP BY MONTH(o.date_added), ot.title";
				break;
			case 'year':
				$sql .= " GROUP BY YEAR(o.date_added), ot.title";
				break;
		}

		$sql .= ") tmp";

		return $this->db->queryOne($sql);
	}
}
?>